package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.results.Code;
import com.itheima.controller.results.Result;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping//添加
    public Result saveStu(Student student){
        boolean flag = studentService.saveStu(student);
        return new Result(flag ? Code.SAVEStu_OK:Code.SAVEStu_ERR);
    }


    @PutMapping//修改
    public Result updateStu(Student student){
        boolean flag = studentService.updateStu(student);
        return new Result(flag ? Code.UPATEStu_OK : Code.UPATEStu_ERR);
    }


    @DeleteMapping("/{uuid}")//删除
    public Result deleteStu(@PathVariable Integer uuid){
        boolean flag = studentService.deleteStu(uuid);
        return new Result(flag ? Code.DELETEStu_OK : Code.DELETEStu_ERR);
    }


    @GetMapping("/{uuid}")//查询单个student
    public Result getStu(@PathVariable Integer uuid){

        Student student =  studentService.getStu(uuid);
        System.out.println(student);
        //使用三元运算符进行判断,并将查询得到的对象传递出去
        return new Result(null != student ? Code.GETStu_OK :Code.GETStu_ERR,student);
    }


    @GetMapping("/{page}/{size}") //分页查询
    public Result getAllStu(@PathVariable Integer page,@PathVariable Integer size){
        PageInfo<Student> all =  studentService.getAllStu(page,size);
        return new Result(null != all ? Code.GETStu_OK :Code.GETStu_ERR,all);
    }

}
